package com.yuyou.fn.common.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.yuyou.fn.common.entity.SiteConfig;
import com.yuyou.fn.common.mapper.SiteConfigMapper;
import com.yuyou.fn.common.service.ISiteConfigService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * SiteConfig 表数据服务层接口实现类
 */
@Service
public class SiteConfigServiceImpl extends SuperServiceImpl<SiteConfigMapper, SiteConfig, Long> implements ISiteConfigService {


    @Override
    //@Cacheable("siteConfig")
    public String getConfigValueFromCache(String configName) {
        return findConfigValue(configName);
    }

    @Override
    public String findConfigValue(String configName) {
        Example<SiteConfig> siteConfigExample = SiteConfig.createExample();
        siteConfigExample.createCriteria().andEqualTo("configName", configName);
        SiteConfig siteConfig = findOneByExample(siteConfigExample);
        if (siteConfig == null) {
            return null;
        } else {
            return siteConfig.getConfigValue();
        }
    }

    @Override
    public void updateConfigValue(String configName, String configValue) {

        Example<SiteConfig> siteConfigExample = SiteConfig.createExample();
        siteConfigExample.createCriteria().andEqualTo("configName", configName);

        SiteConfig siteConfig = new SiteConfig();
        siteConfig.setConfigName(configName);
        siteConfig.setConfigValue(configValue);

        if (!updateSelectiveByExample(siteConfig, siteConfigExample)) {
            insert(siteConfig);
        }


    }
}